package rise.net.handler;

import rise.comunity.Community;
import rise.comunity.Status;
import rise.comunity.User;
import rise.message.StatusNotification;
import rise.ui.Conversation;
import rise.ui.MapActivity;
import rise.ui.UserListActivity;

public class StatusNotificationHandler implements Runnable {
	private StatusNotification statusNotification;

	public StatusNotificationHandler(String packetRappresentation) {
		this.statusNotification = StatusNotification.createStatusNotification(packetRappresentation);
	}

	@Override
	public void run() {
		String sender = getSender();
		Status senderStatus = getSenderStatus();
		double lat = statusNotification.getLat(), lon = statusNotification.getLon();
		
		Community community = Community.obtainCommunity();
		if(!community.contains(sender))
			community.addUser(new User(senderStatus, sender, lat, lon));
		community.setUserStatus(sender, senderStatus);
		community.setUserGPSLocation(sender, lat, lon);
		
		UserListActivity.handler.sendEmptyMessage(0);
		Conversation.handler.sendEmptyMessage(0);
		MapActivity.handler.sendEmptyMessage(0);
	}

	public Status getSenderStatus() {
		return statusNotification.getStatus();
	}

	public String getSender() {
		return statusNotification.getOwner();
	}

}
